package buildTree

func buildTree2(inorder []int, postorder []int) *TreeNode {
	m2 := make(map[int]int, len(inorder))

	for index, v := range inorder {
		m2[v] = index
	}
	var build2 func(inStart, inEnd int) *TreeNode
	build2 = func(inStart, inEnd int) *TreeNode {
		if inStart > inEnd {
			return nil
		}
		val := postorder[len(postorder)-1]
		position := m2[val]

		node := &TreeNode{val: val}
		postorder = postorder[:len(postorder)-1]
		node.right = build2(position+1, inEnd)
		node.left = build2(inStart, position-1)
		return node
	}
	return build2(0, len(postorder)-1)
}
